package cn.edu.ldh.toolapp.slice;

import cn.edu.ldh.toolapp.ResourceTable;
import cn.edu.ldh.toolapp.entity.Merit;
import cn.edu.ldh.toolapp.entity.MeritRes;
import cn.edu.ldh.toolapp.stores.MeritStore;
import cn.edu.ldh.toolapp.util.ToastUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.media.audio.AudioManager;
import ohos.media.audio.SoundPlayer;

public class FishAbilitySlice extends AbilitySlice {

    private SoundPlayer soundPlayer;
    private int soundId;
    private Text meritTxt;
    private int merit;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_fish);
        this.startInit();
        this.getData();
    }

    private void startInit() {
        MeritStore.getInstance().createDataBase(this);
        meritTxt = (Text) findComponentById(ResourceTable.Id_fish_merit_txt);
        findComponentById(ResourceTable.Id_fish_img).setClickedListener(this::fish);
        findComponentById(ResourceTable.Id_fish_merit_save).setClickedListener(this::save);
        findComponentById(ResourceTable.Id_fish_merit_clear).setClickedListener(this::clear);
        findComponentById(ResourceTable.Id_fish_merit_back).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminate();
            }
        });
    }

    //获取数据
    private void getData() {
        MeritRes MeritRes = MeritStore.getInstance().getMerit();
        this.merit = MeritRes.getRes().getMeritNum();
        meritTxt.setText("你的功德" + String.valueOf(this.merit));
    }

    //清除
    private void clear(Component component) {
        this.merit = 0;
        meritTxt.setText("你的功德" + String.valueOf(this.merit));
    }

    //保存
    private void save(Component component) {
        Merit newMerit = new Merit();
        newMerit.setMeritId(MeritStore.getInstance().getMerit().getRes().getMeritId());
        newMerit.setMeritNum(this.merit);
        MeritRes meritRes = MeritStore.getInstance().updateMerit(newMerit);
        ToastUtil.showToast(this, meritRes.getMessage());
    }

    //播放声音
    private void fish(Component component) {
        soundPlayer = new SoundPlayer(AudioManager.AudioVolumeType.STREAM_MUSIC.getValue());
        soundId = soundPlayer.createSound(this, ResourceTable.Media_muyu);
        soundPlayer.setOnCreateCompleteListener((soundPlayer1, cacheId, status) -> {
            // 短音播放，设置音量、循环次数和播放速度
            if (status == 0) {
                int taskId = soundPlayer.play(cacheId);
                soundPlayer.setVolume(taskId, 1.0f);
                soundPlayer.setLoop(taskId, 0); // “-1”表示一直循环播放 0不循环
                soundPlayer.setPlaySpeedRate(taskId, 1.0f);
            }
        });
        meritTxt.setText("你的功德" + String.valueOf(this.merit++));
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        soundPlayer.deleteSound(soundId);
    }
}
